<template>
    <div class="front bj">
        <el-scrollbar style="height: 100%;">
            <el-form :model="updateRowData" label-width="90px" label-position="left" ref="updateRowForm" :rules="updateFormRules">
                <el-form-item label="用户名：" prop="userName">
                    <el-input v-model="updateRowData.userName" @keyup.enter.native="enterKey($event, 'DPT1')"></el-input>
                </el-form-item>
                <el-form-item label="姓名：" prop="name">
                    <el-input v-model="updateRowData.name" ref="DPT1" @keyup.enter.native="enterKey($event, 'DPT2')" readonly></el-input>
                </el-form-item>
                <el-form-item label="部门：" prop="deptName">
                    <el-input v-model="updateRowData.deptName" ref="DPT1" @keyup.enter.native="enterKey($event, 'DPT2')" readonly></el-input>
                </el-form-item>
                <el-form-item label="职务：" prop="positionAux">
                    <el-input v-model="updateRowData.positionAux" ref="DPT1" @keyup.enter.native="enterKey($event, 'DPT2')" readonly></el-input>
                </el-form-item>
                <el-form-item style="margin-bottom: 10px !important;">
                    <el-button type="primary" size="small" @click="save('updateRowForm')">保存</el-button>
                    <el-button type="danger" size="small" @click="close">离开</el-button>
                </el-form-item>
            </el-form>
        </el-scrollbar>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                updateRowData: {
                    userName: '',
                    name: '',
                    deptName: '',
                    positionAux: '',
                    userId: '',
                },
                updateFormRules: {
                    userName: [{required: true, message: '登录名不能为空', triggle: 'blur'}],
                },
                rowId:this.$store.state.userManagementStore.rowId,
                userManagementRow: [],
                index:'',
            }
        },
        methods: {
            getUpdateData(){                    //获取要修改的数据
                this.$http.get(this.$url + 'userManage/findUserById?id=' + this.rowId).then(res => {
                    if(res.data.code == 20001) {
                        if(res.data.data) {
                            this.updateRowData = res.data.data;
                        }
                    }
                });
            },
            getPositionData(row) {          //获取指定位置的数据
                this.updateRowData = row;
            },
            save(formName) {                    //保存
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let data = {
                            id: this.updateRowData['id'],
                            userId: this.updateRowData['userId'],
                            userName: this.updateRowData['userName'],
                            systemId: this.$store.state.systemId
                        }
                        this.$http({
                            url: this.$url + 'userManage/updateUser',
                            method: 'post',
                            params: data
                        }).then(res => {
                            if(res.data.code == 20001) {
                                this.$store.commit('userManagementStore/updateRowId');			//清空state中的rowId
                                this.$message({message: '数据修改成功', type: 'success', duration: 1500});
                                let expandNodeId = this.$store.state.userManagementStore.expandNodeId;
                                setTimeout(() => {
                                    this.$store.dispatch('userManagementStore/getEmployeeDataByDepartmentId', expandNodeId);			//刷新数据
                                    this.$store.dispatch('userManagementStore/getSearch');
                                    this.close();
                                }, 1000);
                            } else {
                                this.$message({message: res.data.msg,type:'error'});
                            }
                        });
                    }
                });
            },
            close() {                           //关闭新增弹窗
                let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
                parent.layer.close(index);
            },
            enterKey(lastRef, nextRef) {		//回车键换行
                if(lastRef.target.value) {
                    this.$refs[nextRef].focus();
                }
            },
        },
        mounted() {
            this.getUpdateData();
        }
    }
</script>